package com.songlanyun.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.modules.account.dao.UserActivationDao;
import com.songlanyun.modules.account.entity.UserActivation;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserActivationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service("UserActivationService")
public class UserActivationServiceImpl extends ServiceImpl<UserActivationDao, UserActivation> implements UserActivationService {

    @Resource
    private UserActivationDao userActivationDao;

    @Resource
    private AccountService accountService;

    @Transactional
    @Override
    public Boolean saveRecord(String s, Long id, String code) {
        // 一、取出前十三个字符
        String firstThirteenCharacters = s.substring(0, 13);
        // 转换成时间
        Long time = Long.parseLong(firstThirteenCharacters);
        // 将时间戳转换为Date对象
        Date date = new Date(time);
//        System.out.println("一、前十三个字符: " + firstThirteenCharacters);
        // 二、删除掉前十三个字符
        String afterDeletingFirstThirteen = s.substring(13);
//        System.out.println("二、删除掉前十三个字符: " + afterDeletingFirstThirteen);
        // 三、取出删除后的前十个字符
        String firstTenAfterDeleting = afterDeletingFirstThirteen.substring(0, 10);
//        System.out.println("三、取出删除后的前十个字符: " + firstTenAfterDeleting);
        // 四、删除掉删除后的字符的前十个字符
        String afterDeletingTen = afterDeletingFirstThirteen.substring(10);
//        System.out.println("四、删除掉删除后的字符的前十个字符: " + afterDeletingTen);
        // 五、检测剩下字符长度
        int remainingLength = afterDeletingTen.length();
//        System.out.println("五、剩下字符长度: " + remainingLength);
//        System.out.println("六、剩下字符: " + afterDeletingTen);
        if (remainingLength == 5) {
            // 存表
            UserActivation userActivation = new UserActivation();
            userActivation.setUserId(id);
            userActivation.setActivationCode(code);
            userActivation.setActivationTime(new Date());
            userActivation.setActivationGenerateTime(date);
            userActivation.setActivationChannel(firstTenAfterDeleting);
            userActivation.setCreatedTime(new Date());
            userActivation.setUpdatedTime(new Date());
            userActivation.setRemarks("");
            int insert = userActivationDao.insert(userActivation);
            // 激活用户
            Boolean aBoolean = accountService.activationUser(id);
            if (aBoolean && insert > 0) {
                return true;
            }
            return false;
        } else {
            return false;
        }
    }

    @Override
    public Boolean checkActivationCode(String code) {
        UserActivation userActivation = userActivationDao.selectOne(new LambdaQueryWrapper<UserActivation>()
                .eq(UserActivation::getActivationCode, code));
        if (userActivation == null) {
            return true;
        }
        return false;
    }
}
